package cn.snmp.mutual.data;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import cn.snmp.mutual.common.tools.StackPrinter;

/**
 * @since 2018.9.16
 * @author dengkn
 * MIB Node 信息节点容器 利用 objLinkListVecs和storeValueMap层级递进，
 * 嵌套ObjService 进行节点mib层级关系展示，配置文件层级关系
 */

public class ObjService implements Cloneable {
	private String clsName;
	private HashMap storeValueMap;
	private LinkedList objLinkListVecs;
	private static StackPrinter printer;

	public ObjService() {
		this("");
	}

	public ObjService(String oname) {
		this(oname, (HashMap) null);
	}

	public ObjService(String oname, HashMap rstore) {
		this(oname, rstore, (LinkedList) null);
	}

	public ObjService(String oname, HashMap<String, String> rstore, LinkedList<LinkedList<ObjService>> vec) {
		this.clsName = oname == null ? "" : oname;
		this.storeValueMap = rstore == null ? new HashMap() : (HashMap) rstore.clone();
		this.objLinkListVecs = vec == null ? new LinkedList() : vec;
	}

	public ObjService(ObjService obj) {
		this.clsName = obj.getClsName() == null ? "" : obj.getClsName();
		this.storeValueMap = obj.getCurrentHashMap() == null ? new HashMap()
				: (HashMap) obj.getCurrentHashMap().clone();
		LinkedList vec = obj.getAllContainedObject();
		if (vec != null && !vec.isEmpty()) {
			this.objLinkListVecs = new LinkedList();
			Iterator iterator = vec.iterator();

			while (true) {
				LinkedList v;
				do {
					do {
						if (!iterator.hasNext()) {
							return;
						}
						v = (LinkedList) iterator.next();
					} while (v == null);
				} while (v.isEmpty());

				LinkedList cloneV = new LinkedList();
				Iterator iterator1 = v.iterator();

				while (iterator1.hasNext()) {
					ObjService objService = (ObjService) iterator1.next();
					cloneV.add(new ObjService(objService));
				}

				this.objLinkListVecs.add(cloneV);
			}
		} else {
			this.objLinkListVecs = new LinkedList();
		}
	}

	public String getClsName() {
		return this.clsName == null ? "" : this.clsName;
	}

	public void setClsName(String clsName) {
		this.clsName = clsName == null ? "" : clsName;
	}

	public void setobjLinkListVecs(LinkedList<LinkedList<ObjService>> v) {
		this.objLinkListVecs = v;
	}

	public HashMap getCurrentHashMap() {
		return this.storeValueMap;
	}

	public void setCurrentHashMap(HashMap table) {
		this.storeValueMap = table;
	}

	public void addContainedObject(ObjService objService) {
		String classname = objService.getClsName();
		LinkedList temp = this.getLinkedListByName(classname);
		if (temp == null) {
			LinkedList vec = new LinkedList();
			vec.add(objService);
			this.objLinkListVecs.add(vec);
		} else {
			temp.add(objService);
		}

	}

	public void addLinkedList(LinkedList linkedList) {
		for (int i = 0; i < linkedList.size(); i++) {
			Object temp = linkedList.get(i);
			if (temp instanceof ObjService) {
				this.addContainedObject((ObjService) temp);
			}
		}

	}

	public void remove(String clsName) {
		LinkedList objects = this.getLinkedListByName(clsName);
		if (objects != null) {
			this.objLinkListVecs.remove(objects);
		}

	}

	public void remove(int column) {
		if (column >= 0 && column < this.objLinkListVecs.size()) {
			this.objLinkListVecs.remove(column);
		}

	}

	public void remove(String clsName, int row) {
		if (this.objLinkListVecs != null) {
			if (row >= 0) {
				LinkedList objects = this.getLinkedListByName(clsName);
				if (objects != null && row < objects.size()) {
					objects.remove(row);
				}

				if (objects != null && objects.size() == 0) {
					this.objLinkListVecs.remove(objects);
				}

			}
		}
	}

	public void remove(int column, int row) {
		if (row >= 0 && column >= 0 && column < this.objLinkListVecs.size()) {
			LinkedList tmp = (LinkedList) this.objLinkListVecs.get(column);
			if (tmp.size() > row) {
				tmp.remove(row);
			}

			if (tmp.size() == 0) {
				this.objLinkListVecs.remove(tmp);
			}

		}
	}

	public int columnSize() {
		return this.objLinkListVecs.size();
	}

	public ObjService objectAt(int i, int j) {
		if (i >= 0 && j >= 0 && i < this.objLinkListVecs.size()) {
			LinkedList<?> tmp = (LinkedList<?>) this.objLinkListVecs.get(i);
			return j < tmp.size() ? (ObjService) tmp.get(j) : null;
		} else {
			return null;
		}
	}

	public int rowSize(int column) {
		if (column >= 0 && column < this.objLinkListVecs.size()) {
			LinkedList<?> tmp = (LinkedList<?>) this.objLinkListVecs.get(column);
			return tmp.size();
		} else {
			return 0;
		}
	}

	public int rowSize(String columnName) {
		return this.objectSize(columnName);
	}

	public ObjService objectAt(String clsName, int l) {
		if (l < 0) {
			return null;
		} else {
			LinkedList objects = this.getLinkedListByName(clsName);
			return objects != null && l < objects.size() ? (ObjService) objects.get(l) : null;
		}
	}

	public int objectSize(String clsName) {
		LinkedList objects = this.getLinkedListByName(clsName);
		return objects == null ? 0 : objects.size();
	}

	public LinkedList getContainedObjSet(String clsName) {
		return this.getLinkedListByName(clsName);
	}

	public LinkedList getLinkedListByName(String name) {
		for (int i = 0; i < objLinkListVecs.size(); i++) {
			LinkedList tmp = (LinkedList) objLinkListVecs.get(i);
			if (tmp.size() > 0) {
				ObjService tmpObj = (ObjService) tmp.get(0);
				if (tmpObj != null && tmpObj.getClsName().equals(name)) {
					return tmp;
				}
			}
		}
		return null;
	}

	public LinkedList getAllContainedObject() {
		return this.objLinkListVecs;
	}

	public LinkedList<ObjService> getAllChildObjects() {
		LinkedList<ObjService> linkedList = new LinkedList<ObjService>();
		Iterator<LinkedList<ObjService>> iterator = this.objLinkListVecs.iterator();

		while (iterator.hasNext()) {
			Object tmp = iterator.next();
			LinkedList<?> children = (LinkedList<?>) tmp;
			Iterator<?> iterator1 = children.iterator();

			while (iterator1.hasNext()) {
				ObjService child = (ObjService) iterator1.next();
				linkedList.add(child);
			}
		}

		return linkedList;
	}

	public LinkedList getLinkedListAt(int i) {
		return i >= 0 && i <= this.objLinkListVecs.size() ? (LinkedList) this.objLinkListVecs.get(i) : null;
	}

	public Object getValue(String attrName) {
		if (this.storeValueMap == null) {
			return null;
		} else {
			Object value = this.storeValueMap.get(attrName);
			if (value != null) {
				return value;
			} else {
				value = this.storeValueMap.get(attrName.toUpperCase());
				if (value != null) {
					if (printer != null) {
						this.print(attrName, attrName.toUpperCase());
					}

					return value;
				} else {
					value = this.storeValueMap.get(attrName.toLowerCase());
					if (value != null) {
						if (printer != null) {
							this.print(attrName, attrName.toLowerCase());
						}

						return value;
					} else {
						if (null == value) {
							for (Object key : storeValueMap.keySet()) {
								if (((String) key).equalsIgnoreCase(attrName)) {
									if (printer != null) {
										this.print(attrName, (String) key);
									}

									return this.storeValueMap.get(key);
								}
							}
						}
					}

					return null;
				}
			}
		}
	}

	private void print(String request, String original) {
		try {
			StringBuilder e = new StringBuilder();
			e.append("Requst = ");
			e.append(request);
			e.append(", ");
			e.append("Original = ");
			e.append(original);
			printer.print(e.toString());
		} catch (Exception arg3) {

		}

	}

	public String getStringValue(String attrName) {
		Object tmp = this.getValue(attrName);
		return tmp == null ? "" : String.valueOf(tmp);
	}

	public int getIntValue(String attrName) {
		Object tmp = this.getValue(attrName);
		return tmp == null ? 0 : Integer.parseInt(String.valueOf(tmp));
	}

	public long getLongValue(String attrName) {
		Object tmp = this.getValue(attrName);
		return tmp == null ? 0L : Long.parseLong(String.valueOf(tmp));
	}

	public double getDoubleValue(String attrName) {
		Object tmp = this.getValue(attrName);
		return tmp == null ? 0.0D : Double.parseDouble(String.valueOf(tmp));
	}

	public void setValue(String attrName, Object value) {
		if (value != null) {
			if (this.storeValueMap == null) {
				this.storeValueMap = new HashMap<String, Object>();
			}
			this.storeValueMap.put(attrName, String.valueOf(value));
		}
	}

	public void setIntValue(String attrName, int value) {
		this.setValue(attrName, Integer.valueOf(value));
	}

	public void setLongValue(String attrName, long value) {
		this.setValue(attrName, Long.valueOf(value));
	}

	public void setDoubleValue(String attrName, double value) {
		this.setValue(attrName, Double.valueOf(value));
	}

	public void clearValue() {
		this.storeValueMap.clear();
	}

	public void dumpHash(HashMap hashmap) {
		StringBuilder sb = new StringBuilder(256);
		for (Object key : hashmap.keySet()) {
			sb.append("key: ").append(key).append("Value: ").append(hashmap.get(key)).append("\r\n");

		}
		System.out.println(sb.toString());
	}

	public void dumpData() {
		System.out.println(this.toString());
	}

	private void dumpWriter(StringBuilder sb, String prefix) {
		sb.append(prefix + "**************************** Objservice Content ****************************");
		sb.append("\r\n");
		sb.append(prefix);
		sb.append("Class Name = " + this.clsName);
		sb.append("\r\n");
		sb.append(prefix);
		sb.append("Follow is storeValueMap : ");
		sb.append("\r\n");
		for (Object key : storeValueMap.keySet()) {
			sb.append(prefix).append("key: ").append(key).append("       ").append("Value:  ").append(this.storeValueMap.get(key));
			sb.append("\r\n");
		}

		if (this.objLinkListVecs != null && this.objLinkListVecs.size() > 0) {
			sb.append(prefix).append("Follow is conatinedObject : ");
			sb.append("\r\n");
			Iterator arg7 = this.objLinkListVecs.iterator();

			while (arg7.hasNext()) {
				Object arg8 = arg7.next();
				LinkedList tmp = (LinkedList) arg8;
				int j = 0;

				for (int size = tmp.size(); j < size; ++j) {
					((ObjService) tmp.get(j)).dumpWriter(sb, prefix + "   ");
					sb.append("\r\n");
				}
			}
		}

	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(1024);
		this.dumpWriter(sb, " ");
		return sb.toString();
	}

	@Override
	public ObjService clone() {
		return new ObjService(this);
	}

	public void copyTo(ObjService obj) {
		if (obj != null) {
			obj.setClsName(this.getClsName());
			obj.setCurrentHashMap(this.storeValueMap == null ? new HashMap() : (HashMap) this.storeValueMap.clone());
			if (this.objLinkListVecs != null && !this.objLinkListVecs.isEmpty()) {
				Iterator iterator = this.objLinkListVecs.iterator();
				while (iterator.hasNext()) {
					Object o = iterator.next();
					LinkedList tmp = (LinkedList) o;
					Iterator iterator1 = tmp.iterator();

					while (iterator1.hasNext()) {
						Object c = iterator1.next();
						obj.addContainedObject((ObjService) c);
					}
				}
			}

		}
	}

	public void reset() {
		this.storeValueMap.clear();
		this.objLinkListVecs.clear();
		this.clsName = "";
	}

	public HashMap getStoreValueMap() {
		return storeValueMap;
	}

	public void setStoreValueMap(HashMap storeValueMap) {
		this.storeValueMap = storeValueMap;
	}

	public LinkedList getObjLinkListVecs() {
		return objLinkListVecs;
	}

	public void setObjLinkListVecs(LinkedList objLinkListVecs) {
		this.objLinkListVecs = objLinkListVecs;
	}

}